﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.linqDAL;
using System.Data.SqlClient;
using dhDataAcess.sysDAL;
using dhDataAcess.baseBLL;
using baseSet;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;


namespace dhsystem.Main
{
    public partial class FrmDataQuery : Form
    {
        queryMasterBLL queryMaster = new queryMasterBLL();
        queryMasterModel queryMasterRow = new queryMasterModel();
        queryBLL querybll = new queryBLL();
        queryModel queryRow = new queryModel();
        itemListBLL itemList = new itemListBLL();
        List<queryModel> queryList = new List<queryModel>();
        basListBLL basList = new basListBLL();

        sysDataSet.queryDataTable queryDetailTable = new sysDataSet.queryDataTable();

        string tableName;
        public DataTable rTable { get; set; }

        public FrmDataQuery(string _tableName)
        {
            InitializeComponent();
            tableName = _tableName;
        }
        //打开窗口时显示的数据
        private void FrmDataQuery_Load(object sender, EventArgs e)
        {
            
            comboBoxstrLog.Text  = "并且";    
            textvalue.Visible = true;
            dateTimePickervalue.Visible = false;
            comboxfield.DataSource = itemList.getitemListBymoduleNum(tableName);
            comboxfield.DisplayMember = "itemDispC";
            comboxfield.ValueMember = "recordid";

            comboxstrBJ.DataSource = basList.getbasListByType("比较符");
            comboxstrBJ.DisplayMember = "basNameII";
            comboxstrBJ.ValueMember = "basNameI";

            bindingSourcequery.DataSource = queryList;
            dGridQuery.DataSource = bindingSourcequery;
            DcolumnSet.colSet(moduleInfo.query, dGridQuery);

            bSourceQueryList.DataSource = queryMaster.getqueryMasterAll(GlobalInfo.UserName, tableName);
            cboQueryList.DataSource = bSourceQueryList;
            cboQueryList.DisplayMember = "queryName";
            cboQueryList.ValueMember = "queryNum";
            cboQueryList.SelectedValue = tableName + GlobalInfo.UserName + "001";
            queryDetailTable = querybll.getquerybyqueryNumAll(tableName + GlobalInfo.UserName + "001");
            dGridQuery.DataSource = queryDetailTable;  //查询最后一次查询

           


           
            
            //找寻系统有没有最后一次查询
           
            //itemListBLL item = new itemListBLL();
            //sysDataSet.itemListDataTable itemList = item.getitemListBymoduleNum(tableName);
            //foreach (sysDataSet.itemListRow itemRow in itemList)
            //{
            //    foreach (DataGridViewRow column in queryDetailTable)
            //    {
            //        if ( == itemRow.itemName)
            //        {
            //            query.strField = itemRow.itemDispC;
            //        }
            //    }
            //}
           
          //  queryNum=tableName+"001";
            
        }
        //添加按钮
        int i = 0;
        private void buttonadd_Click(object sender, EventArgs e)
        {
           // queryNum = tableName + "001";
            try
            {
                string strField = basicSql.itemListByitemName(Convert.ToInt32(comboxfield.SelectedValue.ToString()));
                string strFieldDataType = basicSql.itemListByitemDataType(Convert.ToInt32(comboxfield.SelectedValue.ToString()));
                string strFieldQarm = comboxfield.SelectedValue.ToString();
                foreach (queryModel q in queryList)
                {
                    if (q.strField == strField)
                        strFieldQarm = strFieldQarm + queryList.Count.ToString();
                }

                string strValue = null;
                if (strFieldDataType == "int") strValue = Convert.ToInt32(textvalue.Text).ToString();
                if (strFieldDataType == "nvarchar") strValue = textvalue.Text.ToString();
                if (strFieldDataType == "dateTime") strValue = dateTimePickervalue.Value.ToString();

                //插入数据到集合
                i++;
                queryModel queryM = new queryModel();
                sysDataSet.queryRow query = queryDetailTable.NewqueryRow();
               // query .strFieldName  ="9" ;
                query.strField = strField;
                query.strFieldParm = strFieldQarm;
                query.strBJ = comboxstrBJ.SelectedValue.ToString();
                query.strValue = strValue;
                if (comboBoxstrLog.Text == "并且") query.strLog = " and ";
                if (comboBoxstrLog.Text == "或者") query.strLog = " or ";
                query.strFieldDataType =strFieldDataType ;
                query.createUser = GlobalInfo.UserName;
                query.createDate = DateTime.Now;
                query.mark = "";
                query.querySerial = i;
                query.queryNum = tableName + GlobalInfo.UserName + "001";
                query.userNum = GlobalInfo.UserName;


               // queryM .strFieldName  = "9";
                queryM.strField = strField;
                queryM.strFieldParm = strFieldQarm;
                queryM.strBJ = comboxstrBJ.SelectedValue.ToString();
                queryM.strValue = strValue;
                if (comboBoxstrLog.Text == "并且") queryM.strLog = " and ";
                if (comboBoxstrLog.Text == "或者") queryM.strLog = " or ";
                queryM.strFieldDataType = strFieldDataType ;
                queryM.createUser = GlobalInfo.UserName;
                queryM.createDate = DateTime.Now;
                queryM.mark = "";
                queryM.querySerial = i;
                queryM.queryNum = tableName+GlobalInfo .UserName  + "001";
                queryM.userNum = GlobalInfo.UserName;

                itemListBLL item = new itemListBLL();
                sysDataSet.itemListDataTable itemList = item.getitemListBymoduleNum(tableName);
                foreach (sysDataSet.itemListRow itemRow in itemList)
                {
                    if (query.strField == itemRow.itemName)
                    {
                        query.mark  = itemRow.itemDispC;
                    }
                }
                queryM.mark = query.mark;
                queryList.Add(queryM);
                queryDetailTable.AddqueryRow(query);//添加到查询明细表里面去

                //bindingSourcequery.DataSource = queryList;
                //dGridQuery.DataSource = bindingSourcequery;
               // dGridQuery.DataSource = queryList;
                DcolumnSet.colSet(moduleInfo.query , dGridQuery);
 
            }
            catch (FormatException )
            {
                MessageBox.Show("请输入正确的数据查询格式！！！");
            }
            catch (Exception  ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            
           
           
        }
        //清空查询按钮
        private void buttonclean_Click(object sender, EventArgs e)
        {

            foreach (DataGridViewRow row in dGridQuery.Rows)
            {

                dGridQuery.Rows.Remove(row);
            }
        }
        //查询按钮
        private void buttonquery_Click(object sender, EventArgs e)
        {
            try
            {   

                //先查询系统是否存在最后一次查询
                queryMasterBLL queryMaster = new queryMasterBLL();
                sysDataSet.queryMasterDataTable queryTable = queryMaster.getqueryMasterAll(GlobalInfo.UserName, tableName + GlobalInfo.UserName + "001", tableName);
                string queryNum = null;
                if (queryTable.Rows.Count == 0)  //系统不存在该模块最后一次查询
                {
                    //插入最后一次查询主表和从表记录和从表记录
                    queryMasterRow.createDate = DateTime.Now;
                    queryMasterRow.queryNum = tableName + GlobalInfo.UserName + "001";
                    queryMasterRow.ceateUser = GlobalInfo.UserName;
                    queryMasterRow.queryName = "最后一次查询";
                    queryMasterRow.moduleNum = tableName;
                    queryMasterRow.mark = "";
                    queryMasterRow.userNum = GlobalInfo.UserName;
                    queryNum = tableName +GlobalInfo .UserName + "001";
                }
                else
                {
                    queryNum = cboQueryList.SelectedValue.ToString();
                    queryMasterRow = null;
                    
                }
                querybll.queryAllUpdate(queryDetailTable,queryMasterRow,tableName,queryNum );
                queryDetailTable.AcceptChanges();
                SqlConnection conn = dataConn.connOpen();
                SqlTransaction tran = conn.BeginTransaction();
                Query query = new Query(tran, conn);
                SqlDataReader rdr = query.sqlQuery(basicSql.moduleListSql(tableName), queryNum);
                DataTable dt = new DataTable();
                dt.Load(rdr);
                rTable = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

    
        }


        //字段combox控件属性发生更改时触发的事件
        private void comboxfield_SelectedIndexChanged(object sender, EventArgs e)
        {
            textvalue.Focus();
            try
            {
                string strFieldDataType = basicSql.itemListByitemDataType(Convert.ToInt32(comboxfield.SelectedValue.ToString()));
                if (strFieldDataType == "int" || strFieldDataType == "nvarchar")
                {
                    textvalue.Visible = true;
                    dateTimePickervalue.Visible = false;
                }

                if (strFieldDataType == "dateTime")
                {
                    textvalue.Visible = false;
                    dateTimePickervalue.Visible = true;
                }
            }
            catch (Exception ) { }
        }
        //datagridview第一列显示行号
        private void dGridQuery_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                dGridQuery.Rows.Remove(dGridQuery.CurrentRow);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void dGridQuery_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //保存查询按钮
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (dGridQuery.DataSource == null)
            {
                MessageBox.Show("需要保存的组合查询为空");
            }
            else
            {
                string queryNum;
                if (txtQueryName.Text == "")
                {
                    
                    queryMasterRow.queryNum = cboQueryList.SelectedValue.ToString();
                    queryNum = cboQueryList.SelectedValue.ToString();
                }
                else
                {
                    queryMasterRow.queryNum = tableName + txtQueryName.Text + dGridQuery.RowCount.ToString();
                    queryNum = tableName + txtQueryName.Text + dGridQuery.RowCount.ToString();
                }
                    queryMasterRow.createDate = DateTime.Now;
                    queryMasterRow.ceateUser = GlobalInfo.UserName;
                    queryMasterRow.queryName = txtQueryName.Text;
                    queryMasterRow.moduleNum = tableName;
                    queryMasterRow.mark = "";
                    queryMasterRow.userNum = GlobalInfo.UserName;

                    querybll.queryAllUpdateIII(queryDetailTable, queryMasterRow, queryNum, GlobalInfo.UserName,tableName );
              
                MessageBox.Show("保存查询成功");
                bSourceQueryList.DataSource = queryMaster.getqueryMasterAll(GlobalInfo.UserName, tableName);
                cboQueryList.DataSource = bSourceQueryList;
                cboQueryList.DisplayMember = "queryName";
                cboQueryList.ValueMember = "queryNum";
            }
        }

        private void cboQueryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            queryDetailTable = querybll.getquerybyqueryNumAll(cboQueryList .SelectedValue .ToString ());
            dGridQuery.DataSource = queryDetailTable;
            DcolumnSet.colSet(moduleInfo.query, dGridQuery);
        }
        //删除查询按钮
        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (cboQueryList.SelectedValue.ToString () == tableName + GlobalInfo.UserName + "001")
            {
                MessageBox.Show("不能删除最后一次查询");
            }
            else
            {

                queryMaster.queryMasterDel(cboQueryList .SelectedValue .ToString ());
                bSourceQueryList.DataSource = queryMaster.getqueryMasterAll(GlobalInfo.UserName, tableName);
                cboQueryList.DataSource = bSourceQueryList;
                cboQueryList.DisplayMember = "queryName";
                cboQueryList.ValueMember = "queryNum";
                MessageBox.Show("删除查询成功");
            }
        }
    }
}
